#include "widget.h"
#include "ui_widget.h"

#include <QRegExp>
#include <QPixmap>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->pushButton->setEnabled(false);  // 完成注册按钮不可用
}

bool Widget::isInputContentFormCorrect(QLineEdit *lineedit, const QRegExp &reg)
{
    QString inputCentent = lineedit->text();
    if(reg.indexIn(inputCentent) == -1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

void Widget::showVerifyStatus(bool status, QLabel *hintLabel, QLabel *iconLabel, QString str)
{
    QPixmap right(":/icons/correct.png");
    QPixmap error(":/icons/error.png");
    hintLabel->setAlignment(Qt::AlignLeft);

    if(status)
    {
        iconLabel->setPixmap(right);
        hintLabel->setText(str + "格式正确");
        hintLabel->setStyleSheet("color: green");
    }
    else
    {
        iconLabel->setPixmap(error);
        hintLabel->setText(str + "格式错误");
        hintLabel->setStyleSheet("color: red");
    }
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_telEdit_editingFinished()
{
    bool contentVerify = this->isInputContentFormCorrect(ui->telEdit, this->telReg);
    this->showVerifyStatus(contentVerify, ui->telHintLabel, ui->telHintIcon, "手机号");
}
